package com.kye.express.business.notice.notice_center;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.IView;
import com.kye.express.databinding.ActivityNoticeCenterBinding;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.base.PageInfo;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.notice.NotificationListResultBean;
import com.kye.model.bean.notice.request.NotificationListRequestParam;
import com.kye.model.datasource.NotificationDataSource;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 18/12/2017
 * @updated : 18/12/2017
 * @description : <Description>
 * @update_reason : <UpdateReason>
 */

public interface NoticeCenterContract {

    interface NoticeCenterView extends IView {

        ActivityNoticeCenterBinding getDataBinding();

        void refreshNoticesFirstSuccess(List<NotificationListResultBean.NotificationListItemBean> notificationList);

        void refreshNoticesSuccess(List<NotificationListResultBean.NotificationListItemBean> notificationList);

        void refreshNoticesFailed(Throwable e);

        void loadMoreNoticesSuccess(List<NotificationListResultBean.NotificationListItemBean> notificationList);

        void loadMoreNoticesFailed(Throwable e);

        Context getContext();
    }

    class NoticeCenterPresenter extends BasePresenter<NoticeCenterView> {
        private NotificationDataSource notificationDataSource;
        private PageInfo pageInfo;

        public NoticeCenterPresenter() {
            notificationDataSource = new NotificationDataSource();
            pageInfo = new PageInfo();
        }

        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            if (savedInstanceState != null) {
                pageInfo = savedInstanceState.getParcelable(PageInfo.TAG_PAGE_INFO);
            }
        }

        @Override
        public void onSaveInstanceState(Bundle outState) {
            if (outState != null) {
                outState.putParcelable(PageInfo.TAG_PAGE_INFO, pageInfo);
            }
        }

        /**
         * 上拉加载
         */
        public void LoadMoreNotices() {
            final NotificationListRequestParam notificationListRequestParam = NotificationListRequestParam.newDefaultInstance();
            notificationListRequestParam.setPageNum(pageInfo.getPageIndex() + 1);
            notificationListRequestParam.setPageSize(pageInfo.getPageSize());
            notificationDataSource
                    .getNotificationList(notificationListRequestParam)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<NotificationListResultBean>() {

                        @Override
                        public void onLoading(Disposable d) {
                            addDisposable(d);
                        }

                        @Override
                        public void onSuccess(OpenApiResult<NotificationListResultBean> result, NotificationListResultBean notificationListResultBean) {
                            pageInfo.increasePage();
                            getView().loadMoreNoticesSuccess(notificationListResultBean.getDatas());
                        }

                        @Override
                        public void onApiError(OpenApiResult<NotificationListResultBean> result, int code, String msg) {
                            getView().loadMoreNoticesFailed(null);
                        }

                        @Override
                        public void onLoaded() {

                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().loadMoreNoticesFailed(e);
                        }
                    });
        }

        /**
         * 下拉刷新
         */
        public void refreshNotices() {
            final NotificationListRequestParam notificationListRequestParam = NotificationListRequestParam.newDefaultInstance();
            notificationListRequestParam.setPageNum(PageInfo.INDEX_DEFAULT);
            notificationListRequestParam.setPageSize(PageInfo.SIZE_DEFAULT);
            notificationDataSource
                    .getNotificationList(notificationListRequestParam)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<NotificationListResultBean>() {

                        @Override
                        public void onLoading(Disposable d) {
                            addDisposable(d);
                        }

                        @Override
                        public void onSuccess(OpenApiResult<NotificationListResultBean> result, NotificationListResultBean notificationListResultBean) {
                            pageInfo.resetPage();
                            getView().refreshNoticesSuccess(notificationListResultBean.getDatas());
                        }

                        @Override
                        public void onApiError(OpenApiResult<NotificationListResultBean> result, int code, String msg) {
                            getView().refreshNoticesFailed(null);
                        }

                        @Override
                        public void onLoaded() {

                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().refreshNoticesFailed(e);
                        }
                    });
        }

        /**
         * 首次加载
         */
        public void refreshNoticesFirst() {
            final NotificationListRequestParam notificationListRequestParam = NotificationListRequestParam.newDefaultInstance();
            notificationListRequestParam.setPageNum(pageInfo.getPageIndex());
            notificationListRequestParam.setPageSize(pageInfo.getPageSize());
            notificationDataSource
                    .getNotificationList(notificationListRequestParam)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<NotificationListResultBean>() {

                        @Override
                        public void onLoading(Disposable d) {
                            addDisposable(d);
                        }

                        @Override
                        public void onSuccess(OpenApiResult<NotificationListResultBean> result, NotificationListResultBean notificationListResultBean) {
                            getView().refreshNoticesFirstSuccess(notificationListResultBean.getDatas());
                            pageInfo.increasePage();
                        }

                        @Override
                        public void onApiError(OpenApiResult<NotificationListResultBean> result, int code, String msg) {
                            getView().getDataBinding().statusLayout.showErrorNetworkView();
                        }

                        @Override
                        public void onLoaded() {

                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().getDataBinding().statusLayout.showErrorNetworkView();
                        }
                    });
        }
    }

}
